﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace CalculatorLogic.Algebraic_Math
{
    public class Expand : IOperator
    {
        public Equation Apply(Equation equation)
        {
            List<double> collection = new List<double>();
            if (equation.Input[0].Contains("-"))
            {
                equation.Input[0] = equation.Input[0].Replace("- ", "+-");
            }
            if (Regex.IsMatch(equation.Input[0],@"\(x \+ [0-9]\)\^2"))
            {
                equation.Input[0] = equation.Input[0].Replace("^2", equation.Input[0].TrimEnd(new char[2]{'^','2'}));
            }
            foreach (string entry in equation.Input[0].Replace('(', ' ').Split(')'))
            {
                if (entry.Trim() != "")
                {
                    foreach (string val in entry.Split('+'))
                    {
                        string temp;
                        if (val.Contains("x"))
                        {
                            if (val.IndexOf('x') == 1)
                            {
                                temp = val.Replace('x', '1');
                            }
                            else
                            {
                                temp = val.Replace("x", "*1");
                            }
                            if (temp.Contains("^"))
                            {
                                temp = temp.Remove(val.IndexOf('^'));
                            }
                            if (temp.Contains("*"))
                            {
                                collection.Add((double.Parse(temp.Split('*')[0]) * double.Parse(temp.Split('*')[1])));
                            }
                            else
                            {
                                collection.Add(double.Parse(temp));
                            }
                        }
                        else
                        {
                            collection.Add(double.Parse(val));
                        }
                    }
                }
            }
            if (collection.Count == 4)
            {
                collection = expansion(collection);
                if (collection[0] == 1)
                {
                    equation.Solved = "x^2+" + collection[1] + "*x+" + collection[2];
                }
                else
                {
                    equation.Solved = collection[0] + "*x^2+" + collection[1] + "*x+" + collection[2];
                }
            }
            else
            {
                equation.Solved = equation.Input[0];
            }
            return equation;
        }

        private List<double> expansion(List<double> collection)
        {
            List<double> values = new List<double>();
            values.Add(collection[0] * collection[2]);
            values.Add((collection[0] * collection[3]) + (collection[1] * collection[2]));
            values.Add(collection[1] * collection[3]);
            return values;
        }
    }
}
